"""
服务模块：股票信息
"""

from app.common import SQL_DATETIME_FORMAT
from app.common.exception import InvalidParamException
from app.common.constants import StockInfoConstants
from app.dao.stockinfo import (StockInfoDao, StockInfo)
from app.dto.stockinfo import StockInfoDto


class StockInfoService(object):
    """
    股票信息service类
    """

    @classmethod
    def save(cls, item):
        if not isinstance(item, StockInfoDto):
            raise InvalidParamException("参数错误")
        return StockInfoDao.save(item)

    @classmethod
    def save_all(cls, item_list):
        if not (len(item_list) > 0
                and isinstance(item_list, (list, tuple, set))
                and isinstance(item_list[0], StockInfoDto)):
            raise InvalidParamException("参数错误")
        return StockInfoDao.save_all(item_list)

    @classmethod
    def remove_by_id(cls, id_):
        item = StockInfoService.find_by_id(id_=id_)
        if not item:
            raise InvalidParamException("id为{id_}的股票信息不存在".format(id_=id_))

        return StockInfoDao.remove_by_id(id_)

    @classmethod
    def update(cls, item):
        if not isinstance(item, StockInfoDto):
            raise InvalidParamException("参数错误")

        return StockInfoDao.update(item)

    @classmethod
    def find_by_id(cls, id_):
        item = StockInfoDao.find_by_id(id_)
        return cls.__convert_to_view(item)

    @classmethod
    def find_all(cls):
        result = list()
        item_list = StockInfoDao.find_all()
        if item_list:
            for item in item_list:
                result.append(cls.__convert_to_view(item))

        return result

    @classmethod
    def page_by_param(cls, page=1, page_size=10, **kwargs):
        page_data = StockInfoDao.page_by_param(page, page_size, **kwargs)
        items = page_data['items']

        new_items = []
        for item in items:
            result = cls.__convert_to_view(item)
            if result:
                new_items.append(result)
        page_data['items'] = new_items

        return page_data

    @classmethod
    def __convert_to_view(cls, item):
        """
        转化成视图对象
        :param item:
        :return:
        """

        result = None
        if isinstance(item, StockInfo):
            modify_time = item.modify_time
            modify_time_str = modify_time.strftime(SQL_DATETIME_FORMAT) if modify_time else ""

            type_ = int(item.type)
            status = int(item.status)

            result = dict(id=int(item.id),
                          code=item.code,
                          name=item.name,
                          type=type_,
                          type_str=StockInfoConstants.Type.get_value_name(type_),
                          status=status,
                          status_str=StockInfoConstants.Status.get_value_name(status),
                          add_time_str=item.add_time.strftime(SQL_DATETIME_FORMAT),
                          modify_time_str=modify_time_str,
                          )
        return result
